package com.briup.bookstore.web.controller;

import com.briup.bookstore.common.model.entity.Order;
import com.briup.bookstore.common.model.param.PageParam;
import com.briup.bookstore.common.model.vo.OrderVO;
import com.briup.bookstore.common.response.Result;
import com.briup.bookstore.common.util.PageUtil;
import com.briup.bookstore.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

    /**
     * 订单模块Service对象
     */
    private final OrderService orderService;

    /**
     * 分页+条件检索订单信息
     *
     * @param pageNum   当前页
     * @param pageSize  每页大小
     * @param id 订单号
     * @param status 订单状态
     * @return
     */
    @GetMapping(params = "page=true")
    public Result pageQuery(@RequestParam(value = "pageNum") int pageNum,
                            @RequestParam(value = "pageSize") int pageSize,
                            @RequestParam(value = "id", required = false) String id,
                            @RequestParam(value = "status", required = false) Integer status) {
        /* 封装分页参数 */
        PageParam pageParam = new PageParam()
                .pageNum(pageNum)
                .pageSize(pageSize);
        /* 封装查询条件参数 */
        Order orderParam = new Order()
                .setId(id)
                .setStatus(status);
        return Result.ok(PageUtil.convert(
                orderService.pageQueryByClause(pageParam, orderParam),
                OrderVO::toVO));
    }

    /**
     * 根据ID查询订单信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") String id) {
        return Result.ok(OrderVO.toVO(orderService.getById(id)));
    }

    /**
     * 关闭订单
     * @param id
     * @return
     */
    @PutMapping(params = "action=close")
    public Result close(@RequestBody String id) {
        orderService.close(id);
        return Result.ok();
    }

    /**
     * 订单发货
     * @param orderParam
     * @return
     */
    @PutMapping(params = "action=delivery")
    public Result delivery(@RequestBody Order orderParam) {
        orderService.delivery(orderParam);
        return Result.ok();
    }

}
